package com.zimi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimi.mapper.SysFunctionMapper;
import com.zimi.model.SysFunction;
import com.zimi.model.SysUser;
import com.zimi.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 系统功能表 Service
 *
 * @author lnintr
 * @since 2019-05-26
 */
@Service
public class SysFunctionService {
    @Autowired
    private SysFunctionMapper sysFunctionMapper;

    /**
     * author wh
     *
     * @param sysFunction
     * @return
     */
    public Object getList(SysFunction sysFunction) {
        List<Integer> idList = new ArrayList<>();
        idList.add(1);
        idList.add(3);
        List<SysFunction> list = sysFunctionMapper.selectList(new QueryWrapper<SysFunction>().lambda().in(SysFunction::getFunctionState, idList).orderByAsc(SysFunction::getFunctionSort));
        return list;
    }

    public Object insert(SysFunction sysFunction) {
        SysUser user = UserUtil.getUser();

        sysFunction.setFunctionState(1);
        sysFunction.setCreateUser(user.getId());
        sysFunction.setCreateTime(new Date());

        return sysFunction.insert();
    }

    @Transactional
    public void update(List<SysFunction> sysFunctions) {
        // 1）对要修改的对象加行锁
        if (sysFunctions.size() > 0) {
            List<Integer> ids = new ArrayList<>();
            Map<Integer, SysFunction> functionMap = new HashMap<>();
            for (SysFunction sf : sysFunctions) {
                ids.add(sf.getId());

                functionMap.put(sf.getId(), sf);
            }

            List<SysFunction> result = sysFunctionMapper.selectList(new QueryWrapper<SysFunction>().lambda().in(SysFunction::getId, ids).last("for update"));

            // 2）判断该数据已经被改过没有
            if (result != null && result.size() > 0) {
                SysFunction tmpFunction = null;
                SysUser user = UserUtil.getUser();
                Date updateTime = new Date();

                for (SysFunction sf : result) {
                    tmpFunction = functionMap.get(sf.getId());

                    if (sf.getUpdateTime() != null) {
                        if (tmpFunction.getUpdateTime() == null) {
                            throw new RuntimeException("修改数据失败，数据已经被其他用户改过。");
                        } else {
                            if (tmpFunction.getUpdateTime().getTime() != sf.getUpdateTime().getTime()) {
                                throw new RuntimeException("修改数据失败，数据已经被其他用户改过。");
                            }
                        }
                    }

                    // 3)变更数据
                    tmpFunction.setUpdateUser(user.getId());
                    tmpFunction.setUpdateTime(updateTime);

                    tmpFunction.updateById();
                }
            } else {
                throw new RuntimeException("修改数据失败，数据已经被其他用户改过。");
            }
        }
    }
}
